<?php
class home extends ujn_controller {
	function index() { // show login
		if($data = mdl_account::get_instance()->get_current()) {
			if(!empty($data) && isset($data['id'])) {
				tls_func::redirect('index.php/category.index');
			}
		}
		
		$this->assign('msg', '');
		if($this->method == 'POST') {
			if(mdl_account::get_instance()->check_login($this->post('username'), $this->post('userpswd'))) {
				tls_func::redirect('index.php/category.index');
			} else {
				$this->assign('msg', '帐密错误');
			}
		}
	}
	
	function logout() {
		mdl_account::get_instance()->logout();
		tls_func::redirect('index.php/home.index');
	}
	
	function hit() {
		$this->auto_render = false;
		$id = $this->get('id', 0);
		$catid = $this->get('catid', 0);
		if(empty($id) || empty($catid)) die('0');
		mdl_news::get_instance()->hit(1, $id, $catid);
		echo '1';
	}
	
	function downloads() { // 动态下载页
		$id = $this->get('id', 0);
		$d = mdl_news::get_instance()->get($id);

		//最新资料下载
		$dx = mdl_category::get_instance()->get(10002);
		$d_cat = explode(',', $dx['arrchildid']);
		$dowx = mdl_category::get_instance()->find_downloads($d_cat, $siteid,6);
		$this->assign('dowx', $dowx);

		//最新口语节目
		$nx = mdl_category::get_instance()->get(2058);
		$n_cat = explode(',', $nx['arrchildid']);
		$nowx = mdl_category::get_instance()->find_sub($n_cat, $siteid,6);
		$this->assign('nowx', $nowx);
		
		//判断是否存在
		$flrc = "http://www.kekenet.com/".$d['data']['fileurl'].".lrc";
		if (!@fopen($flrc,"r")){
			$flrc = '';
		}
		$plrc = "http://x.kekenet.com/qita/pdf/kekenet_".$id.".pdf";
		if(!@fopen($plrc,"r")){
			$plrc = '';
		}
		
		// 前缀可自行调整
		$this->assign('mp3_file', $d['data']['fileurl']);
		//$this->assign('mp3_file2', $d['data']['fileurl']);
		$this->assign('lrc_file', $flrc);
		$this->assign('pdf_file', $plrc);
	}
	
	function scws() {
		$this->auto_render = false;
		$this->view_type = 'json';
		
		$str = $this->get('str', '');
		$d = tls_func::rest_request_autobuildpostdata('post', 'http://www.ftphp.com/scws/api.php', null, array(
				'data' => $str,
				'respond' => 'json',
				'charset' => 'utf8',
				'ignore' => 'yes',
				'multi' => '3'
				));
		$d = $d['data'];
		$strs = array();
		if($d['status'] == 'ok') {
			foreach($d['words'] as $x) {
				$strs[] = $x['word'];
			}
		}
		$this->assign('strs', $strs);
		return array(); // $d['data'];
	}
}